from py2neo import Graph, Node, Relationship, NodeMatcher
import pandas as pd
import csv
# 连接neo4j数据库，输入地址、用户名、密码
# coding:utf-8
graph = Graph("http://127.0.0.1:7474",auth=("neo4j","Gx1398229566"))
graph.delete_all()
matcher_1 = NodeMatcher(graph)
with open('D:\AQAS\Gx\GetXml\Test2\downloaded.csv', 'r',encoding='utf-8') as f:
    reader = csv.reader(f)
    data = list(reader)
print(data[1])
#['0', '汤庸', '教授/学者网创始人', '华南师范大学', '计算机科学与技术', 'www.scholat.com/ytang']
for i  in  range(1,len(data)):
    node = Node('Denoter',name = data[i][3],id = data[i][0],time= data[i][1])
    print(node)
    graph.create(node)
    if(data[i][2]):
        # print("Location:"+data[i][2])
        relation5 = Node('Location',name = data[i][2])
        graph.create(relation5)
        Location = Relationship(node, '地点', relation5)
        graph.create(Location)
    if(data[i][4]):
        relation1 = Node('Participant__001',name = data[i][4])
        graph.create(relation1)
        Participant1 = Relationship(node, '参与者1', relation1)
        graph.create(Participant1)
    if(data[i][5]):
        relation2 = Node('Participant__002', name=data[i][5])
        graph.create(relation2)
        Participant2 = Relationship(node, '参与者2', relation2)
        graph.create(Participant2)
    if(data[i][8]):
        relation3 = Node('Object',name = data[i][8])
        graph.create(relation3)
        Object = Relationship(node, '对象', relation3)
        graph.create(Object)
    if (data[i][6]):
        relation4 = Node('Participant', name=data[i][6])
        graph.create(relation4)
        Participant3 = Relationship(node, '参与者3', relation4)
        graph.create(Participant3)
with open('nRelation.csv', 'r',encoding='utf-8') as f:
    reader = csv.reader(f)
    data = list(reader)
print(data[1])
#['0', '汤庸', '教授/学者网创始人', '华南师范大学', '计算机科学与技术', 'www.scholat.com/ytang']
for i  in  range(1,len(data)):
    if(data[i][2]=='Causal'):
        node1 = matcher_1.match('Denoter').where("_.id="+"'"+data[i][3]+"'").first()
        print(data[i][3]+"  "+data[i][4])
        print(node1)
        node2 = matcher_1.match('Denoter').where("_.id="+"'"+data[i][4]+"'").first()
        CausalRelation = Relationship(node1,'因果',node2)
        graph.create(CausalRelation)

print("hello world")
